package com.workhours.controller;

import com.workhours.service.HolidayService;
import com.workhours.vo.HolidayVO;
import com.workhours.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 节假日控制器
 * 
 * @author WorkHours
 * @since 2025-09-19
 */
@Tag(name = "节假日管理", description = "节假日信息获取和管理")
@RestController
@RequestMapping("/api/holidays")
@RequiredArgsConstructor
public class HolidayController {

    private final HolidayService holidayService;

    /**
     * 获取指定年份的节假日数据
     * 
     * @param year 年份，默认为当前年份
     * @return 节假日列表
     */
    @Operation(summary = "获取年度节假日", description = "获取指定年份的所有节假日信息，包括法定节假日和调休工作日")
    @GetMapping("/{year}")
    public Result<List<HolidayVO>> getHolidaysByYear(
            @Parameter(description = "年份", example = "2025")
            @PathVariable Integer year) {
        
        if (year < 2020 || year > 2030) {
            return Result.error("年份范围应在2020-2030之间");
        }
        
        List<HolidayVO> holidays = holidayService.getHolidaysByYear(year);
        return Result.success("获取成功", holidays);
    }

    /**
     * 获取当前年份的节假日数据
     * 
     * @return 当前年份的节假日列表
     */
    @Operation(summary = "获取当前年度节假日", description = "获取当前年份的所有节假日信息")
    @GetMapping("/current")
    public Result<List<HolidayVO>> getCurrentYearHolidays() {
        int currentYear = java.time.LocalDate.now().getYear();
        List<HolidayVO> holidays = holidayService.getHolidaysByYear(currentYear);
        return Result.success("获取成功", holidays);
    }

    /**
     * 刷新指定年份的节假日数据
     * 
     * @param year 年份
     * @return 刷新后的节假日列表
     */
    @Operation(summary = "刷新年度节假日", description = "删除缓存并重新从提莫API获取指定年份的节假日数据")
    @PostMapping("/{year}/refresh")
    public Result<List<HolidayVO>> refreshHolidaysByYear(
            @Parameter(description = "年份", example = "2025")
            @PathVariable Integer year) {
        
        if (year < 2020 || year > 2030) {
            return Result.error("年份范围应在2020-2030之间");
        }
        
        List<HolidayVO> holidays = holidayService.refreshHolidaysByYear(year);
        return Result.success("刷新成功", holidays);
    }
}
